package org.cuit.lmq.controller;


import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.domain.common.BaseContextHolder;
import org.cuit.lmq.domain.common.Result;
import org.cuit.lmq.domain.po.User;
import org.cuit.lmq.service.BodyInfoService;
import org.cuit.lmq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Seon
 * @since 2023-03-03
 */
@Slf4j
@RestController
@RequestMapping("/user-info")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private BodyInfoService bodyInfoService;

    @PutMapping("/update/information")
    public Result<?> updateUserInfo(@RequestBody User user) {
        try {
            long userId = BaseContextHolder.getCurrentUserId();
            user.setId(userId);
            userService.updateById(user);
            return Result.success(true);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新失败，请稍后重试", 0);
        }
    }

    @GetMapping("/data")
    public Result<?> getUserInformation() {
        long userId = BaseContextHolder.getCurrentUserId();
        User user = userService.getById(userId);
        return Result.success(user);
    }

    @GetMapping("/body/statistics")
    public Result<?> statisticsBodyInfo() {
        Map<String, List<List<String>>> data = bodyInfoService.statisticsBodyInfo();
        return Result.success(data);
    }

    @GetMapping("/body/peak")
    public Result<?> peakBodyInfo() {
        Map<String, String> data = bodyInfoService.peakBodyInfo();
        return Result.success(data);
    }
}

